(function(){

'use strict';
/**
 * @ngdoc RMqueryRoleModule
 * @name cmmdApp  
 * @systemManger 聂开枥
 */
angular.module('controllers.faultLocation.alarmStatisticAnalysis')
  .controller('endDetailedCtr', ['$scope', 'endAnalysisServer', '$mdDialog', '$filter', 'CoreService', 'endDetailedServer','$state',endDetailedCtr])
  .controller('tableDataCtr', ['$scope', 'endAnalysisServer', '$mdDialog', '$filter', '$state', 'CoreService', tableData]);
/**
 *  下拉选项
 *  给默认初始值时间为当前时间 
 *  省份,业务名称,系统名称,显示方式为请选择字样,【组建需修改,前台无法与控制器双向绑定】
 */
function endDetailedCtr($scope, endAnalysisServer, $mdDialog, $filter, CoreService, endDetailedServer,$state) {
  $scope.param = {};
  $scope.param = endDetailedServer.param;
  //查询业务名称
  $scope.querybusiness = function() {
    var item = {};
    item.systemCode = $scope.param.systemCode;
    return endAnalysisServer.businessQuery(item).then(function(result) {
      $scope.business = result.data;
    });
  };
  //省份查询:给省份默认代码
  endAnalysisServer.provinceQuery(function(result) {
    $scope.provinces = result.data;
  });

  endAnalysisServer.systemQuery(function(result) {
    $scope.systems = result.data;
  });
  $scope.changeSystem = function(systemCode) {
    $scope.querybusiness();
    if (systemCode === '11') {
      $scope.param.businessCode = '01';
    }
    $scope.param.businessCode = '12';
  }

  if(endAnalysisServer.getitems().type!=='1'){
    detailed();

  }


  //查询方法
  $scope.dropSw = function() {
    if ($scope.tradeForm.$invalid) {
      return;
    }
    if (Boolean($scope.shoumun)) {
      detailed();
    } else {
      $scope.shoumun = 1;
    }
  };
  $scope.shoumun = 0;
  $scope.currentPage = 1;
  $scope.pageSize = '10';

  //查询详细信息
  function detailed() {
    var serialNum = $scope.param.serialNum === undefined ? '' : $scope.param.serialNum;
    $scope.param.serialNum = serialNum;
    $scope.param.sortState = $scope.param.sortState && 1;
    $scope.param.page = {
      currentPage: 1,
      pageSize: '10'
    };
    $scope.param.sortState = '1';
    endAnalysisServer.detQuery($scope.param, function(result) {
      if (result.state === '10000' && result.data.pageLine.length > 0) {
        $scope.$broadcast('detQueryResult', result.data, $scope.param);
        $scope.shoumun = 0;
      } else {
        CoreService.dialog('没有数据', '提示');
      }
    });
  }
  $scope.returnAnalysis = function() {
    $scope.items={};
    $scope.items.type = '0';
    endAnalysisServer.setitems($scope.items);
    $state.go('sas.faultLocation.endAnalysis.analysis');
  }
}



function tableData($scope, endAnalysisServer, $mdDialog, $filter, $state, CoreService) {



  $scope.$on('detQueryResult', function(e, d, param) {
    viewData(d, param);
  });

  if (endAnalysisServer.getitems().type === '1') {
    var serialNum = $scope.param.serialNum === undefined ? '' : $scope.param.serialNum;
    $scope.param.serialNum = serialNum;
    $scope.param.sortState = '1';
    viewData(endAnalysisServer.getitems(), $scope.param);
  }

  function viewData(d, param) {
    $scope.params = param;
    $scope.totalCount = d.totalCount;
    $scope.currentPage = param.page.currentPage;
    $scope.pageSize = param.page.pageSize;
    $scope.items = d;
  }

  $scope.sort = function() {
    if ($scope.param.sortState === '0') {
      $scope.param.sortState = '1';
    } else {
      $scope.param.sortState = '0';
    }
    detailedQuery($scope.param);
  };



  function detailedQuery(param) {
    endAnalysisServer.detQuery(param, function(result) {
      if (result.state === '10000' && result.data.pageLine.length > 0) {
        viewData(result.data, param);
        $scope.shoumun = 0;
      } else {
        CoreService.dialog('没有数据', '提示');
      }
    });
  }


  /**
   * 查询详细对比方法 由图形下钻得到
   * 给分页定义数据默认1:10
   * 格式化时间操作yyyyMMdd
   */

  $scope.sumturnToPage = function(param) {
    detailedQuery(param);
  };



  $scope.compare = function(compare) {
    endAnalysisServer.setMessage(compare);
    $scope.items.type = '1';
    endAnalysisServer.setitems($scope.items);
    $state.go('sas.faultLocation.endAnalysis.components');
  };

}

})();